package com.huawei.bitfun;

import com.huawei.deveco.projectmodel.ohos.model.ModuleModel;
import com.huawei.deveco.projectmodel.ohos.model.ProjectModelManager;
import com.huawei.deveco.projectmodel.ohos.util.ProjectUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

import java.util.List;
import java.util.Optional;

public class ConfigProvider {
    private Project project;

    private ConfigProvider() {}

    public void setProject(Project project) {
        this.project = project;
    }

    public Project getProject() {
        return this.project;
    }

    public ModuleModel getModuleModel(VirtualFile virtualFile) {
        if (project == null) {
            return null;
        }
        return Optional.ofNullable(virtualFile)
                .map(VirtualFile::getCanonicalPath)
                .map(path -> ProjectUtil.getModuleModelByCanonicalPath(
                    ProjectModelManager.getInstance().getTargetProjectModel(project), path
                )).orElse(null);
    }

    public List<ModuleModel> getModules() {
        return ProjectModelManager.getInstance().getTargetProjectModel(project).getModuleModelList();
    }

    public List<ModuleModel> getNativeModules() {
        var modules = getModules();
        return modules.stream().filter(ModuleModel::isNativeModule).toList();
    }

    public static ConfigProvider getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private static class InstanceHolder {
        private static final ConfigProvider INSTANCE = new ConfigProvider();
    }
}